import time

from sqlalchemy.orm import sessionmaker

import settings

from sqlalchemy import Column, Integer, Text, String, DateTime, create_engine, Boolean, ForeignKey, TIMESTAMP
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('mysql://{}:{}@{}:{}/{}?charset=utf8'.format(
    settings.MysqlConfig['user'],
    settings.MysqlConfig['password'],
    settings.MysqlConfig['host'],
    settings.MysqlConfig['port'],
    settings.MysqlConfig['db']), echo=False, pool_size=8, pool_recycle=60 * 30)

Base = declarative_base()

Session = sessionmaker(bind=engine)
DBSession = Session()


class BaseMixin:
    id = Column(Integer, primary_key=True)
    created_at = Column(Integer, default=int(time.time()), comment='创建日期')
    updated_at = Column(Integer, default=int(time.time()), onupdate=int(time.time()), comment='更新日期')


class GrabConfig(Base, BaseMixin):
    __tablename__ = 'fa_grab_config'

    sensitive_words_cate = Column(String(255), comment='敏感词分类设置')
    website_area = Column(String(255), comment='网站地址设置')
    weibo_cate = Column(String(255), comment='微博分类设置')
    official_account_type = Column(String(255), comment='公众号类型设置')
    user_id = Column(Integer, comment='用户ID')


class SensitiveWordCategory(Base, BaseMixin):
    __tablename__ = 'fa_sensitive_words_category'

    cate_name = Column(String(50), comment='分类名称')


class SensitiveWord(Base, BaseMixin):
    __tablename__ = 'fa_sensitive_words'

    cid = Column(Integer, comment='分类id')
    words = Column(String(50), comment='敏感词')
    right_words = Column(String(50), comment='正确表述')


class WebSite(Base, BaseMixin):
    __tablename__ = 'fa_website'

    filing_type = Column(Integer, comment='网站类型（网站分类）')
    website_name = Column(String(50), comment='网站名称')
    website_domain = Column(String(255), comment='网站域名')
    is_access = Column(String(1), default='1', comment='是否可以访问')
    ip_address = Column(String(20), comment='ip地址')
    ip_location = Column(String(100), comment='ip所在地')
    detectioned_at = Column(String(100), comment='检测时间')
    filing_license_no = Column(String(50), comment='备案许可证号')
    sponsor = Column(String(50), comment='主办者')
    place_business = Column(String(255), comment='营业地')
    office_address = Column(String(255), comment='办公地址')
    access_point = Column(String(255), comment='接入地')
    early_warning_info = Column(Text, comment='预警信息')
    area = Column(String(50), comment='地区')
    user_id = Column(Integer, index=True, comment='用户ID')

    @staticmethod
    def fetchall():
        return DBSession.query(WebSite).all()


class WebSiteLib(Base, BaseMixin):
    __tablename__ = 'fa_website_lib'

    website_label = Column(String(50), comment='网站标识')
    website_link = Column(String(50), comment='网站链接')
    grab_time = Column(Integer, default=int(time.time()), comment='抓取时间')
    snapshot_address = Column(String(255), comment='快照地址')
    response_code = Column(String(10), comment='响应代码')
    belong_website = Column(String(50), comment='所属网站')
    status = Column(String(1), comment='爬取状态：0-未爬取，1-爬取中，2-已完成，3-不可用')
    website_domain_id = Column(Integer, index=True, comment='属地网站管理外键')
    user_id = Column(Integer, index=True, comment='用户ID')

    def fetchall(self):
        ret = DBSession.query(WebSiteLib).all()
        return ret


class VioContent(Base, BaseMixin):
    __tablename__ = 'fa_vio_content'

    main = Column(String(255), comment='主体')
    title = Column(String(255), comment='标题')
    link = Column(Text, comment='违规链接')
    category = Column(String(50), comment='涉及类别')
    word = Column(String(255), comment='涉及词')
    right_word = Column(String(255), comment='正确表述')
    content = Column(Text, comment='违规关键词的上下文')
    snapshot = Column(Text, comment='快照')

    website_lib_id = Column(Integer, index=True, comment='网址库id')
    website_id = Column(Integer, index=True, comment='属地网站管理id')
    user_id = Column(Integer, index=True, comment='用户ID')

# Base.metadata.create_all(bind=engine)
